package com.mcxx.modules.common.controller;

import com.mcxx.modules.common.service.impl.IntegrateCheckServiceImpl;
import com.mcxx.modules.miniSalvation.check.dto.DepartmentDataDTO;
import com.mcxx.modules.miniSalvation.check.param.DepartmentDataSearchParam;
import com.mcxx.modules.miniSalvation.check.service.read.DepartmentDataReadService;
import com.mcxx.modules.miniSalvation.check.service.write.DepartmentDataWriteService;
import com.mcxx.modules.miniSalvation.family.dto.MessageDTO;
import com.mcxx.result.BaseResult;
import com.mcxx.service.DefaultRestService;
import com.mcxx.util.*;
import com.yinhai.ta404.component.audit.vo.ExcelInfo;
import com.yinhai.ta404.core.restservice.annotation.RestService;
import com.yinhai.ta404.core.restservice.requestbean.PageParam;
import com.yinhai.ta404.core.restservice.resultbean.Page;
import com.yinhai.ta404.core.utils.ResponseExportUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;



@Validated
@Api("横向部门数据核对控制类")
@RestService({"department/check"})
public class DepartmentCheckController extends DefaultRestService {
    @Autowired
    private DepartmentDataReadService dataReadService;
    @Autowired
    private DepartmentDataWriteService dataWriteService;
    @Autowired
    private IntegrateCheckServiceImpl integrateCheckService;

    private static final Logger logger = LoggerFactory.getLogger(DepartmentCheckController.class);

    @PostMapping("queryDataList")
    @ApiOperation("查询横向部门数据列表")
    public void queryDataList(DepartmentDataSearchParam param, PageParam pageParam){
        logger.info("查询横向部门数据列表,参数：{};分页参数：{}",param,pageParam);
        Page page = dataReadService.selectDepartmentDataList(param, pageParam, getCurUserAccount());
        setPageBean(page);
        logger.info("查询横向部门数据列表完成");
    }

    @PostMapping("downloadDataList")
    @ApiOperation(value="导出横向部门数据", produces = MediaType.APPLICATION_JSON_UTF8_VALUE,notes="导出横向部门数据")
    public void downloadDataList(HttpServletResponse response, @RequestBody DepartmentDataSearchParam param)throws IOException {
        logger.info("导出横向部门数据查询列表，参数是：{}", param);
        ExcelInfo excelInfo = dataWriteService.downLoadDepartmentData(param,getCurUserAccount());
        if(null != excelInfo){
            FileExportUtil.exportFileResponse(response, excelInfo, getCurUserAccount().getAreaName());
//            ZipInfoUtil.exportZipResponse(response, excelInfo, this.getCurUserId());
        }
        logger.info("导出横向部门数据查询列表，查询结果是：{}", excelInfo);
    }

    @PostMapping({"downLoadImportTemplate"})
    public void downLoadBankTemplate(HttpServletResponse response) throws IOException {
        logger.info("下载横向部门数据导入模板开始");
        InputStream resourceAsStream = this.getClass().getResourceAsStream("/bakhtml/横向部门数据导入模板.xls");
        byte[] bytes = IOUtils.toByteArray(resourceAsStream);
        resourceAsStream.close();
        ResponseExportUtil.exportFileWithBytes(response,bytes,"横向部门数据导入模板.xls");
        logger.info("下载横向部门数据导入模板结束");
    }

    @PostMapping(value = "importDepartmentData")
    @ApiOperation(value = "批量导入横向部门数据")
    public void importIntoBankAccount(@RequestParam("file") MultipartFile file) {
        logger.info("批量导入低收入家庭信息开始");
        if (CommonUtil.invalidArgs(file)) {
            setError(FailureCode.ERR_10000.getMessage());
        }
        BaseResult<MessageDTO> result= null;
        try {
            result = dataWriteService.importDepartmentData(file,file.getInputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
        setData(result);
        logger.info("批量导入低收入家庭信息结束{}",result);
    }

    @PostMapping("sendCheck")
    @ApiOperation("横向部门发起核对")
    public void sendCheck(@RequestPart List<DepartmentDataDTO> checkDTOList){
        logger.info("横向部门发起核对，参数是：{}", checkDTOList);
        String res  = integrateCheckService.sendDepartmentCheck(checkDTOList, getCurUserAccount());
        setData(res);
        logger.info("横向部门发起核对，结果是：{}", res);
    }

    @PostMapping("deleteObject")
    @ApiOperation(value="删除横向部门数据")
    public void deleteObject(@RequestParam(value="ids") List<String> ids) {
        logger.info("开始删除横向部门数据，参数是：{}", ids);
        String msg = dataWriteService.deletObjectData(ids);
        setData(msg);
        logger.info("删除横向部门数据结束，查询结果是：{}", msg);
    }
}
